package com.zzyl.nursing.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.nursing.domain.NursingLevel;
import com.zzyl.nursing.mapper.NursingLevelMapper;
import com.zzyl.nursing.service.INursingLevelService;
import com.zzyl.nursing.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 护理等级Service业务层处理
 * 
 * @author alexis
 * @date 2025-06-02
 */
@Service
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService
{
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    //生成redis缓存，方便加载数据的时候快速从缓存中获取，不要每次都去数据库里面查
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;


    /**
     * 查询护理等级
     * 
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询护理等级列表
     * 
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevel> selectNursingLevelList(NursingLevel nursingLevel)
    {
        return nursingLevelMapper.selectNursingLevelList(nursingLevel);
    }

    /**
     * 新增护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel)
    {
        //删除缓存
        deleteCache();
        boolean flag = save(nursingLevel);
        return flag ? 1 : 0;
    }


    /**
     * 修改护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel)
    {
        deleteCache();
        boolean flag = updateById(nursingLevel);
        return flag ? 1 : 0;
    }

    /**
     * 批量删除护理等级
     * 
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids)
    {
        //删除缓存
        deleteCache();
        boolean flag = removeByIds(Arrays.asList(ids));
        return flag ? 1 : 0;
    }

    /**
     * 删除护理等级信息
     * 
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id)
    {
        //删除缓存
        deleteCache();
        boolean flag = removeById(id);
        return flag ? 1 : 0;
    }

    /**
     * 查询护理等级Vo列表
     *
     * @param nursingLevel 条件
     * @return 结果
     */
    @Override
    public List<NursingLevelVo> selectNursingLevelVoList(NursingLevel nursingLevel) {
        return nursingLevelMapper.selectNursingLevelVoList(nursingLevel);
    }

    /**
     * 查询所有护理等级
     *  1）查询缓存 ,如果缓存有数据 直接返回
     *  2）如果缓存没有数据，查询数据库，将查询的数据写入到缓存中，返回数据
     * @return 结果
     */
    @Override
    public List<NursingLevel> listAll() {
        //1）查询缓存 ,如果缓存有数据 直接返回
        List<NursingLevel> list = (List<NursingLevel>) redisTemplate.opsForValue().get("nursingLevel:all");
        //如果查出来的数据不为空（利用hutool工具）就把缓存数据进行返回给前端
        if (CollUtil.isNotEmpty(list)){
            return list;
        }
        //2）如果缓存没有数据，查询数据库，将查询的数据写入到缓存中，返回数据
        LambdaQueryWrapper<NursingLevel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NursingLevel::getStatus, 1);
        //将查出来的数据赋值给list
        list =  list(queryWrapper);
        if (CollUtil.isNotEmpty(list)){
            //如果查出来的数据不为空，将数据添加到缓存中
            redisTemplate.opsForValue().set("nursingLevel:all",list);
        }
        return list;
    }

    //定义一个缓存清除方法，便于在数据更改的时候进行数据同步
    public void deleteCache(){
        redisTemplate.delete("nursingLevel:all");
    }
}
